home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Development Tools & Languages / Macintosh Common Lisp Related / Tutorials / clos-guide.txt next >
Encoding:
Text File  |  1993-02-26  |  14.0 KB  |  464 lines  |  [TEXT/ttxt]

  1. Brief Guide to CLOS
  2.  
  3. Jeff Dalton, University of Edinburgh, <J.Dalton@ed.ac.uk>
  4.  
  5. Contents:
  6.  
  7.   1. Defining classes
  8.   2. Instances.
  9.   3. Inheritance of slot options
  10.   4. Multiple inheritance
  11.   5. Generic functions and methods
  12.   6. Method combination
  13.   7. Quick reference
  14.  
  15. [Think of upper case in things like DEFCLASS as literals -- I
  16. don't mean to imply that actual upper case would be used.  "Thing*"
  17. means zero or more occurrences of thing; "thing+" means one or more.
  18. Curly brackets { and } are used for grouping, as in {a b}+, which
  19. means a b, a b a b, a b a b a b, etc.]
  20.  
  21.  
  22. 1. Defining classes.
  23.  
  24. You define a class with DEFCLASS:
  25.  
  26.    (DEFCLASS class-name (superclass-name*)
  27.      (slot-description*)
  28.      class-option*)
  29.  
  30. For simple things, forget about class options.
  31.  
  32. A slot-description has the form (slot-name slot-option*), where each
  33. option is a keyword followed by a name, expression, or whatever.  The
  34. most useful slot options are
  35.  
  36.    :ACCESSOR function-name
  37.    :INITFORM expression
  38.    :INITARG symbol
  39.  
  40. (Initargs are usually keywords.)
  41.  
  42. DEFCLASS is similar to DEFSTRUCT.  The syntax is a bit different, and
  43. you have more control over what things are called.  For instance,
  44. consider the DEFSTRUCT:
  45.  
  46.    (defstruct person
  47.      (name 'bill)
  48.      (age 10))
  49.  
  50. DEFSTRUCT would automatically define slots with expressions to
  51. compute default initial values, access-functions like PERSON-NAME
  52. to get and set slot values, and a MAKE-PERSON that took keyword
  53. initialization arguments (initargs) as in
  54.  
  55.    (make-person :name 'george :age 12)
  56.  
  57. A DEFCLASS that provided similar access functions, etc, would be:
  58.  
  59.    (defclass person ()
  60.      ((name :accessor person-name
  61.             :initform 'bill
  62.             :initarg :name)
  63.       (age :accessor person-age
  64.            :initform 10
  65.            :initarg :age)))
  66.  
  67. Note that DEFCLASS lets you control what things are called.  For
  68. instance, you don't have to call the accessor PERSON-NAME.  You could
  69. call it NAME.
  70.  
  71. In general, you should pick names that make sense for a group of
  72. related classes rather than rigidly following the DEFSTRUCT
  73. conventions.
  74.  
  75. You do not have to provide all options for every slot.
  76. Maybe you don't want it to be possible to initialize a slot
  77. when calling MAKE-INSTANCE (for which see below).  In that case,
  78. don't provide an :INITARG.  Or maybe there isn't a meaningful
  79. default value.  (Perhaps the meaningful values will always be
  80. specified by a subclass.)  In that case, no :INITFORM.
  81.  
  82. Note that classes are objects.  To get the class object from its
  83. name, use (FIND-CLASS name).  Ordinarily, you won't need to do this.
  84.  
  85.  
  86. 2. Instances
  87.  
  88. You can make an instance of a class with MAKE-INSTANCE.  It's
  89. similar to the MAKE-x functions defined by DEFSTRUCT but lets you
  90. pass the class to instantiate as an argument:
  91.  
  92.    (MAKE-INSTANCE class {initarg value}*)
  93.  
  94. Instead of the class object itself, you can use its name.
  95. For example:
  96.  
  97.    (make-instance 'person :age 100)
  98.  
  99. This person object would have age 100 and name BILL, the default.
  100.  
  101. It's often a good idea to define your own constructor functions,
  102. rather than call MAKE-INSTANCE directly, because you can hide 
  103. implementation details and don't have to use keyword parameters
  104. for everything.  For instance, you might want to define
  105.  
  106.   (defun make-person (name age)
  107.     (make-instance 'person :name name :age age))
  108.  
  109. if you wanted the name and age to be required, positional parameters,
  110. rather than keyword parameters.
  111.  
  112. The accessor functions can be used to get and set slot values:
  113.  
  114.    <cl> (setq p1 (make-instance 'person :name 'jill :age 100))
  115.    #<person @ #x7bf826> 
  116.  
  117.    <cl> (person-name p1)
  118.    jill 
  119.  
  120.    <cl> (person-age p1)
  121.    100 
  122.  
  123.    <cl> (setf (person-age p1) 101)
  124.    101 
  125.  
  126.    <cl> (person-age p1)
  127.    101 
  128.  
  129. Note that when you use DEFCLASS, the instances are printed using
  130. the #<...> notation, rather than as #s(person :name jill :age 100).
  131. But you can change the way instances are printed by defining methods
  132. on the generic function PRINT-OBJECT.
  133.  
  134. Slots can also be accessed by name using (SLOT-VALUE instance slot-name):
  135.  
  136.    <cl> (slot-value p1 'name)
  137.    jill 
  138.  
  139.    <cl> (setf (slot-value p1 'name) 'jillian)
  140.    jillian 
  141.  
  142.    <cl> (person-name p1)
  143.    jillian 
  144.  
  145. You can find out various things about an instance by calling
  146. DESCRIBE:
  147.  
  148.    <cl> (describe p1)
  149.    #<person @ #x7bf826> is an instance of class 
  150.         #<clos:standard-class person @ #x7ad8ae>:
  151.    The following slots have :INSTANCE allocation:
  152.    age     101
  153.    name    jillian
  154.  
  155.  
  156. 2. Inheritance of slot options
  157.  
  158. The class above had no superclass.  That's why there was a "()" after
  159. "defclass person".  Actually, this means it has one superclass: the
  160. class STANDARD-OBJECT.
  161.  
  162. When there are superclasses, a subclass can specify a slot that has
  163. already been specified for a superclass.  When this happens, the
  164. information in slot options has to be combined.  For the slot options
  165. listed above, either the option in the subclass overrides the one in
  166. the superclass or there is a union:
  167.  
  168.    :ACCESSOR  --  union
  169.    :INITARG   --  union
  170.    :INITFORM  --  overrides
  171.  
  172. This is what you should expect.  The subclass can _change_ the
  173. default initial value by overriding the :INITFORM, and can add
  174. to the initargs and accessors.
  175.  
  176. However, the "union" for accessor is just a consequence of how
  177. generic functions work.  If they can apply to instances of a
  178. class C, they can also apply to instances of subclasses of C.
  179.  
  180. (Accessor functions are generic.  This may become clearer
  181. once generic functions are discussed, below.)
  182.  
  183. Here are some subclasses:
  184.  
  185.    <cl> (defclass teacher (person)
  186.           ((subject :accessor teacher-subject
  187.             :initarg :subject)))
  188.    #<clos:standard-class teacher @ #x7cf796> 
  189.  
  190.    <cl> (defclass maths-teacher (teacher)
  191.           ((subject :initform "Mathematics")))
  192.    #<clos:standard-class maths-teacher @ #x7d94be> 
  193.  
  194.    <cl> (setq p2 (make-instance 'maths-teacher
  195.                     :name 'john
  196.                     :age 34))
  197.    #<maths-teacher @ #x7dcc66> 
  198.  
  199.    <cl> (describe p2)
  200.    #<maths-teacher @ #x7dcc66> is an instance of
  201.         class #<clos:standard-class maths-teacher @ #x7d94be>:
  202.     The following slots have :INSTANCE allocation:
  203.     age        34
  204.     name       john
  205.     subject    "Mathematics"
  206.  
  207.  
  208. Note that classes print like #<clos:standard-class maths-teacher @ #x7d94be>.
  209. The #<...> notation usually has the form
  210.  
  211.    #<class-of-the-object ... more information ...>
  212.  
  213. So an instance of maths-teacher prints as #<MATHS-TEACHER ...>.
  214. The notation for the classes above indicates that they are
  215. instances of STANDARD-CLASS.  DEFCLASS defines standard classes.
  216. DEFSTRUCT defines structure classes.
  217.  
  218.  
  219. 4. Multiple inheritance
  220.  
  221. A class can have more than one superclass.  With single inheritance
  222. (one superclass), it's easy to order the superclasses from most to
  223. least specific.  This is the rule:
  224.  
  225. Rule 1: Each class is more specific than its superclasses.
  226.  
  227. In multiple inheritance this is harder.  Suppose we have
  228.  
  229.   (defclass a (b c) ...)
  230.  
  231. Class A is more specific than B or C (for instances of A), but what if
  232. something (an :INITFORM, or a method) is specified by B and C?  Which
  233. overrides the other?  The rule in CLOS is that the superclasses listed
  234. earlier are more specific than those listed later.  So:
  235.  
  236. Rule 2: For a given class, superclasses listed earlier are more
  237.         specific than those listed later.
  238.  
  239. These rules are used to compute a linear order for a class and all its
  240. superclasses, from most specific to least specific.  This order is the
  241. "class precedence list" of the class.
  242.  
  243. The two rules are not always enough to determine a unique order,
  244. however, so CLOS has an algorithm for breaking ties.  This ensures
  245. that all implementations always produce the same order, but it's
  246. usually considered a bad idea for programmers to rely on exactly
  247. what the order is.  If the order for some superclasses is important,
  248. it can be expressed directly in the class definition.
  249.  
  250.  
  251. 5. Generic functions and methods
  252.  
  253. Generic function in CLOS are the closest thing to "messages".
  254. Instead of writing
  255.  
  256.   (SEND instance operation-name arg*)
  257.  
  258. you write
  259.  
  260.   (operation-name instance arg*)
  261.  
  262. The operations / messages are generic functions -- functions whose
  263. behavior can be defined for instances of particular classes by
  264. defining methods.
  265.  
  266. (DEFGENERIC function-name lambda-list) can be used to define a generic
  267. function.  You don't have to call DEFGENERIC, however, because DEFMETHOD
  268. automatically defines the generic function if it has not been defined
  269. already.  On the other hand, it's often a good idea to use DEFGENERIC
  270. as a declaration that an operation exists and has certain parameters.
  271.  
  272. Anyway, all of the interesting things happen in methods.  A method is
  273. defined by:
  274.  
  275.    (DEFMETHOD generic-function-name specialized-lambda-list
  276.      form+)
  277.  
  278. This may look fairly cryptic, but compare it to DEFUN described in
  279. a similar way:
  280.  
  281.    (DEFUN function-name lambda-list form+)
  282.  
  283. A "lambda list" is just a list of formal parameters, plus things like
  284. &OPTIONAL or &REST.  It's because of such complications that we say
  285. "lambda-list" instead of "(parameter*)" when describing the syntax.
  286.  
  287. [I won't say anything about &OPTIONAL, &REST, or &KEY in methods.
  288. The rules are in CLtL, if you want to know them.]
  289.  
  290. So a normal function has a lambda list like (var1 var2 ...).
  291. A method has one in which each parameter can be "specialized"
  292. to a particular class.  So it looks like:
  293.  
  294.   ((var1 class1) (var2 class2) ...)
  295.  
  296. The specializer is optional.  Omitting it means that the method
  297. can apply to instances of any class, including classes that were
  298. not defined by DEFCLASS.  For example:
  299.  
  300.    (defmethod change-subject ((teach teacher) new-subject)
  301.      (setf (teacher-subject teach) new-subject))
  302.  
  303. Here the new-subject could be any object.  If you want to restrict
  304. it, you might do something like:
  305.  
  306.    (defmethod change-subject ((teach teacher) (new-subject string))
  307.      (setf (teacher-subject teach) new-subject))
  308.  
  309. Or you could define classes of subjects.
  310.  
  311. Methods in "classical" object-oriented programming specialize
  312. only one parameter.  In CLOS, you can specialize more than one.
  313. If you do, the method is sometimes called a multi-method.
  314.  
  315. A method defined for a class C overrides any method defined
  316. for a superclass of C.  The method for C is "more specific"
  317. than the method for the superclass, because C is more specific
  318. that the classes it inherits from (eg, dog is more specific
  319. than animal).
  320.  
  321. For multi-methods, the determination of which method is more
  322. specific involves more than one parameter.  The parameters
  323. are considered from left to right.
  324.  
  325.    (defmethod test ((x number) (y number))
  326.      '(num num))
  327.   
  328.    (defmethod test ((i integer) (y number))
  329.      '(int num))
  330.  
  331.    (defmethod test ((x number) (j integer))
  332.      '(num int))
  333.  
  334.    (test 1 1)      =>  (int num), not (num int)
  335.    (test 1 1/2)    =>  (int num) 
  336.    (test 1/2 1)    =>  (num int) 
  337.    (test 1/2 1/2)  =>  (num num) 
  338.  
  339.  
  340. 6. Method combination.
  341.  
  342. When more than one class defines a method for a generic function, and
  343. more than one method is applicable to a given set of arguments, the
  344. applicable methods are combined into a single "effective method".
  345. Each individual method definition is then only part of the definition
  346. of the effective method.
  347.  
  348. One kind of method combination is always supported by CLOS.  It is
  349. called standard method combination.  It is also possible to define
  350. new kinds of method combination.  Standard method combination
  351. involves four kinds of methods:
  352.  
  353.   * Primary methods form the main body of the effective method.
  354.     Only the most specific primary method is called, but it can
  355.     call the next most specific primary method by calling
  356.  
  357.       (call-next-method)
  358.  
  359.   * :BEFORE methods are all called before the primary method, with
  360.     the most specific :BEFORE method called first.
  361.  
  362.   * :AFTER methods are all called after the primary method, with
  363.     the most specific :AFTER method called last.
  364.  
  365.   * :AROUND methods run before the other methods.  As with
  366.     primary methods, only the most specific is called and the
  367.     rest can be invoked by CALL-NEXT-METHOD.  When the least
  368.     specific :AROUND method calls CALL-NEXT-METHOD, what it
  369.     calls is the combination of :BEFORE, :AFTER, and primary
  370.     methods.
  371.  
  372. :BEFORE, :AFTER, and :AROUND methods are indicated by putting the
  373. corresponding keyword as a qualifier in the method definition.
  374. :BEFORE and :AFTER methods are the easiest to use, and a simple
  375. example will show how they work:
  376.  
  377.    (defclass food () ())
  378.  
  379.    (defmethod cook :before ((f food))
  380.      (print "A food is about to be cooked."))
  381.  
  382.    (defmethod cook :after ((f food))
  383.      (print "A food has been cooked."))
  384.  
  385.    (defclass pie (food)
  386.      ((filling :accessor pie-filling :initarg :filling :initform 'apple)))
  387.  
  388.    (defmethod cook ((p pie))
  389.      (print "Cooking a pie")
  390.      (setf (pie-filling p) (list 'cooked (pie-filling p))))
  391.  
  392.    (defmethod cook :before ((p pie))
  393.      (print "A pie is about to be cooked."))
  394.  
  395.    (defmethod cook :after ((p pie))
  396.      (print "A pie has been cooked."))
  397.  
  398.    (setq pie-1 (make-instance 'pie :filling 'apple))
  399.  
  400. And now:
  401.  
  402.    <cl> (cook pie-1)
  403.    "A pie is about to be cooked." 
  404.    "A food is about to be cooked." 
  405.    "Cooking a pie" 
  406.    "A food has been cooked." 
  407.    "A pie has been cooked." 
  408.    (cooked apple)
  409.  
  410.  
  411. 7. Quick reference
  412.  
  413. Square brackets ("[" and "]") indicate optional elements.  A vertical
  414. bar ("|") indicates an alternative.  Thing* means zero or more
  415. occurrence of thing, thing+ means one or more occurrence.  "{" and "}"
  416. are used for grouping.
  417.  
  418. Defining a class:
  419.  
  420.    (DEFCLASS class-name (superclass-name*)
  421.      (slot-description*))
  422.  
  423. Slot descriptions:
  424.  
  425.    (slot-name slot-option*)
  426.  
  427.    :ACCESSOR function-name
  428.    :INITFORM expression
  429.    :INITARG keyword-symbol
  430.  
  431. Making instances:
  432.  
  433.    (MAKE-INSTANCE class {initarg value}*)
  434.  
  435. Method definitions:
  436.  
  437.    (DEFMETHOD generic-function-name [qualifier] specialized-lambda-list
  438.      form+)
  439.  
  440. where
  441.  
  442.    generic-function-name is a symbol;
  443.  
  444.    qualifier is :BEFORE, :AFTER, :AROUND, or else omitted;
  445.  
  446.    specialized-lambda-list is
  447.       ( {variable | (variable class-name)}* )
  448.  
  449. Some functions:
  450.  
  451.    (DESCRIBE instance)
  452.    (DESCRIBE class)
  453.  
  454.    (FIND-CLASS class-name) -> class
  455.  
  456.    (CLASS-NAME class) -> symbol
  457.  
  458.    (CLASS-PRECEDENCE-LIST class) -> list of classes
  459.  
  460.    (CLASS-DIRECT-SUPERCLASSES class) -> list of classes
  461.  
  462.    (CLASS-DIRECT-SUBCLASSES class) -> list of classes
  463.  
  464.